package com.example.mqtt.service.impl;

import com.example.mqtt.common.enums.QueueStatus;
import com.example.mqtt.config.MessageQueueManager;
import com.example.mqtt.config.MessageStorage;
import com.example.mqtt.entity.QueueInfo;
import com.example.mqtt.entity.QueueSequence;
import com.example.mqtt.mapper.QueueSequenceMapper;
import com.example.mqtt.model.Card;
import com.example.mqtt.service.QueueService;
import com.example.mqtt.service.SmsService;
import com.example.mqtt.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Map;
import java.util.UUID;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/5/4 23:18
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class QueueServiceImpl implements QueueService {
    private final MessageStorage messageStorage;
    private final MessageQueueManager messageQueueManager;
    private final WebSocketService webSocketService;
    private final QueueSequenceMapper queueSequenceMapper;
    private final SmsService smsService;
    @Override
    public void pushQueue(QueueInfo queueInfo) {
        messageQueueManager.put(queueInfo);
        messageStorage.storeQueue(queueInfo.getCardId(), queueInfo);
        Card card = Card.builder()
                .id(queueInfo.getCardId())
                .contact(queueInfo.getPhone())
                .name(queueInfo.getUsername())
                .build();
        webSocketService.sendAddCard(card);

        QueueSequence queueSequence = QueueSequence.builder()
                .sequenceId(UUID.randomUUID().toString())
                .username(queueInfo.getUsername())
                .phone(queueInfo.getPhone())
                .rfidTag(queueInfo.getCardId() + queueInfo.getCardName())
                .serviceStatus(QueueStatus.QUEUEING.getCode())
                .serviceDate(LocalDate.now().toString())
                .build();
        queueSequenceMapper.saveQueueSequence(queueSequence);
    }

    @Override
    public void popQueue(QueueInfo queueInfo) {
        String cardId = queueInfo.getCardId();
        // 发生消息时，用户处于排队中，发送短信
        if (queueInfo.getQueueStatus().equals(QueueStatus.QUEUEING.getCode())) {
            QueueInfo top = messageQueueManager.peek();
            if (top.getCardId().equals(queueInfo.getCardId())) {
                // 队顶元素与mqtt的cardId一致，通知用户
                smsService.sendMessage(top.getPhone());
                log.info("当前用户"+ top.getUsername() + "，排队号码为："+ cardId + "，联系号码为：" + top.getPhone());
            }
        } else {
            // 中断排队，完成排队等，从队顶取出数据
            QueueInfo polled = messageQueueManager.poll();
            log.info("当前用户"+ polled.getUsername() + "，对应的排队号码为："+ cardId);
            if (polled.getCardId().equals(cardId)) {
                QueueStatus queueStatus = QueueStatus.fromCode(queueInfo.getQueueStatus());
                // 通知前端页面
                webSocketService.sendRemoveTopCard();
                // 持久化至数据库
                queueSequenceMapper.updateQueueSeqStatus(cardId + queueInfo.getCardName() , queueStatus.getCode());
                log.info("mqtt排队号码为："+ cardId + "，最终排队状态为：" + queueStatus.getMessage());
            }
        }

    }
}
